package com.jason.basicandroid.ui.menu;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.TextView;

public class MenuDemo1 extends Activity {
	Menu myMenu = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    	TextView tv = (TextView)findViewById(R.id.tvShow);
    	registerForContextMenu(tv);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	
    	myMenu = menu;
    	
    	addRegularMenuItems(myMenu);
    	    	
    	return true;
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    	if(R.id.tvShow == v.getId()) {
    		menu.setHeaderTitle("MenuDemo1");
    		menu.add(300, 300, 300, "Context Menu 1");
    	}
    }
    
    private void addRegularMenuItems(Menu menu) {
    	int base = Menu.FIRST; // value is 1
    	
    	MenuItem item = menu.add(base, base, base, "append");
    	item.setIcon(R.drawable.icon);
    	
    	menu.add(base, base+1, base+1, "item 2");
    	menu.add(base, base+2, base+2, "clear");
    	
    	addControlSubMenu(menu);

    	addSecondarySubMenu(menu);
    }
    
    private void addControlSubMenu(Menu menu) {
    	int base = Menu.FIRST + 100;
    	SubMenu sm = menu.addSubMenu(base, base, base, "Control");

    	sm.add(base, base+1, base+1, "hide secondary");
    	sm.add(base, base+2, base+2, "show secondary");
    	
    	sm.add(base, base+3, base+3, "enable secondary");
    	sm.add(base, base+4, base+4, "disable secondary");
    	
    	sm.add(base, base+5, base+5, "check secondary");
    	sm.add(base, base+6, base+6, "uncheck secondary");
    }
    
    private void addSecondarySubMenu(Menu menu) {
    	int base = Menu.CATEGORY_SECONDARY;
    	menu.add(base, base+1, base+1, "sec. item 1");
    	menu.add(base, base+2, base+2, "sec. item 2");
    	menu.add(base, base+3, base+3, "sec. item 3");
    	menu.add(base, base+4, base+4, "sec. item 4");
    	menu.add(base, base+5, base+5, "sec. item 5");
    	menu.add(base, base+6, base+6, "sec. item 6");
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	int id = item.getItemId();
    	
    	switch(id) {
    	case 1:
    		appendText("\nhello");
    		break;

    	case 2:
    		appendText("\nitem 2");
    		
    		break;

    	case 3:
    		emptyText();
    		
    		break;

    	case 102:
    		appendMenuItemText(item);
    		myMenu.setGroupVisible(Menu.CATEGORY_SECONDARY, false);
    		
    		break;

    	case 103:
    		appendMenuItemText(item);
    		myMenu.setGroupVisible(Menu.CATEGORY_SECONDARY, true);
    		
    		break;

    	case 104:
    		appendMenuItemText(item);
    		myMenu.setGroupEnabled(Menu.CATEGORY_SECONDARY, true);
    		
    		break;

    	case 105:
    		appendMenuItemText(item);
    		myMenu.setGroupEnabled(Menu.CATEGORY_SECONDARY, false);
    		
    		break;

    	case 106:
    		appendMenuItemText(item);
    		myMenu.setGroupCheckable(Menu.CATEGORY_SECONDARY, true, false);
    		
    		break;

    	case 107:
    		appendMenuItemText(item);
    		myMenu.setGroupCheckable(Menu.CATEGORY_SECONDARY, false, false);
    		
    		break;
    		
		default:
			return super.onOptionsItemSelected(item);
    	}
    	
    	return true;
    }
    
    @Override 
    public boolean onContextItemSelected(MenuItem item) {
    	if(300 == item.getItemId()) {
    		appendMenuItemText(item);
    	}
    	return true;
    }
    
    private void appendText(String str) {
    	TextView tv = (TextView)findViewById(R.id.tvShow);
    	tv.setText(tv.getText() + str);
    }
    
    private void appendMenuItemText(MenuItem item) {
    	appendText("\n" + item.getTitle().toString());
    }
    
    private void emptyText() {
    	TextView tv = (TextView)findViewById(R.id.tvShow);
    	tv.setText("");
    }
}